var map,
SHP_layer1,SHP_layer2;//定义矢量图层
var SHP_url="http://ec2-52-80-7-103.cn-north-1.compute.amazonaws.com.cn:8090/iserver/services/map-Maoer_SHP/rest/maps/";
var maoer_data_url= "http://ec2-52-80-7-103.cn-north-1.compute.amazonaws.com.cn:8090/iserver/services/data-maoer_shp/rest/data";
var maoer_Host_url="http://ec2-52-80-7-103.cn-north-1.compute.amazonaws.com.cn:8090/iserver/services/map-ThematicMap/rest/maps/";

var urlmaoer_name=SHP_url+"maoer_name";

var map = L.map('map', {
    center: [45.37, 127.6],
    maxZoom: 18,
    zoom: 11,
    zoomControl: false
});
//帽儿山专题地图图层
var maoer_theme_map1 = L.supermap.tiledMapLayer(maoer_Host_url+"data_201803@gutan",{
    transparent:true,
    opacity:1,
    cacheEnabled:true
});
var maoer_theme_map2 = L.supermap.tiledMapLayer(maoer_Host_url+"data_201803@gutan",{
    transparent:true,
    opacity:1,
    cacheEnabled:true
});
SHP_layer1 =L.supermap.tiledMapLayer(SHP_url+"data_201803@gutan",{
    transparent:true,
    opacity:0,
    zIndex:1
});
SHP_layer2 =L.supermap.tiledMapLayer(SHP_url+"data_201803@gutan",{
    transparent:true,
    opacity:0,
    zIndex:1
});
//林场名称图层
var maoer_theme_name=L.supermap.tiledMapLayer(urlmaoer_name,{
    transparent:true,
    opacity:1,
    zIndex:10
});
init()

/***************************************************************封装函数*********************************************************************************************/


//初始图层，全局初始化
function init(){
    //添加底图
    default_map.addTo(map);
    google_map.addTo(map);
    google_satellite.addTo(map);
    gaode_satellite.addTo(map);
    // gaodebiaozhu.addTo(map);
    //添加专题图
    maoer_theme_map1.addTo(map);
    maoer_theme_map2.addTo(map);
    SHP_layer1.addTo(map);
    SHP_layer2.addTo(map);
    maoer_theme_name.setZIndex(10);
    loadComponents()
}
/*********************************************矢量地图切换****************************************************/
var resultLayer;

function add_SHP(index_rov, time, index_name){
    
    if(map.hasLayer(resultLayer)){
        map.removeLayer(resultLayer);
    }
    SHP_layer_tem=L.supermap.tiledMapLayer(SHP_url+index_rov+'_'+time+'@'+index_name,{
        transparent:true,
        opacity:1,
        zIndex:1
    });
    
    vector_query_init();
    SHP_layer_tem.addTo(map)
    maoer_theme_name.setZIndex(10);
    gaodebiaozhu.setZIndex(2)

    if(map.hasLayer(SHP_layer1)){
        map.removeLayer(SHP_layer1);
        SHP_layer1 = SHP_layer2;
        SHP_layer2 = SHP_layer_tem
    }
}
//移除矢量图层
function remove_SHP(){
    if(map.hasLayer(SHP_layer1)){
        SHP_layer1.setOpacity(0)
    }
    
    if(map.hasLayer(SHP_layer2)){
        SHP_layer2.setOpacity(0)
    }
    if(map.hasLayer(resultLayer)){
        map.removeLayer(resultLayer);
    }
    $('.footbtn-shilia').css('display','block')  
    $('.nofootbtn').css('display','none') 
    $('.btnList-content-three').css('display','none')
    $('.btnList>ul>li:nth-child(3)').removeClass('activei')
}
//矢量图层，地图与地图进行交互
function vector_query_init() {
    var idsParam = new SuperMap.GetFeaturesByIDsParameters({
        IDs: [1,2,3,4,5,6,7,8,9,10],//为超图为每个控件对象设置的默认SmID值
        datasetNames:["maoer_shp:maoer_4326"]//数据需要为4326投影方式，读取的json对象为经纬度坐标
    });
    L.supermap
        .featureService(maoer_data_url)
        .getFeaturesByIDs(idsParam, function (serviceResult) {
            // var str = JSON.stringify(serviceResult.result.features)
            if(map.hasLayer(resultLayer)){
                map.removeLayer(resultLayer);
            }
            resultLayer = L.geoJSON(serviceResult.result.features, {
                style:function(feature){
                    // console.log(feature.properties.color);
                    return {color:"transparent"};
                },
                onEachFeature:onEachFeature
            });
            resultLayer.addTo(map);
        });
}


//绑定鼠标事件——矢量
function onEachFeature(feature, layer) {
    layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
        click: popToFeature,
        add: init_layer
    });
}

//图层加载-----------加载所有数据并弹窗，
//此处由于popToFeature的bug，第一次点击事件中无法弹窗，因此在图层加载的时候先进行一次预弹出，但是不显示，弥补后面的点击bug
function init_layer(e){
    var layer = e.target;
    console.log(layer.feature.properties)
    layer.bindPopup("ID: " + layer.feature.properties.林区名 +"<br>" + "面积" + "：" + layer.feature.properties.AREA);
}
//鼠标事件--------鼠标进入高亮
function highlightFeature(e) {
    var layer = e.target;

    layer.setStyle({
        weight: 4,
        color: '00EEEE',
        dashArray: '',
        fillOpacity: 0.7
    });

    if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
        layer.bringToFront();
    }
}
//鼠标事件--------鼠标移出恢复
function resetHighlight(e) {
    resultLayer.resetStyle(e.target);
}
//鼠标事件--------鼠标点击弹窗
function popToFeature(e) {
    var layer = e.target;
    allname = layer.feature.properties
    let arr = []
    arr.push(allname.林区名)
    console.log(arr,'我要的值')
    allData(arr)
    layer.bindPopup("ID: " + layer.feature.properties.林区名 +"<br>" + "面积" + "：" + layer.feature.properties.AREA);
}
//专题图样式
function style(feature) {
    return {
        fillColor: getColor(feature.properties.area),
        weight: 2,
        opacity: 1,
        color: 'white',
        dashArray: '3',
        fillOpacity: 0.7
    };
}

/********************************************栅格专题图切换*****************************************************/
function changeraster(index_rov, time, index_name ){
    /**
     * 参数说明：
     * @index_rov 表示显示实物量还是价值量，实物为固定字符串‘Data’，价值量为固定字符串‘value’
     * @time 表示显示的时间，对于有时间区分度的数据为年月组合，如2018年1月为‘201801’， 
     *       对没有时间区分度的数据为固定字符串‘null’
     * @index_name 表示当前选择的指标，
     */

    if(map.hasLayer(resultLayer)){
        map.removeLayer(resultLayer);
    }
    

    maoer_theme_map_temp = L.supermap.tiledMapLayer(maoer_Host_url+index_rov+'_'+time+'@'+index_name,{
        transparent:true,
        opacity:1
    });


    maoer_theme_map_temp.addTo(map);

    maoer_theme_name.setZIndex(10);//设置林场名称图层zindex为10

    

    if(map.hasLayer(maoer_theme_map1)){
        maoer_theme_map1.removeFrom(map);
        maoer_theme_map1 = maoer_theme_map2
        maoer_theme_map2 = maoer_theme_map_temp
    }
    if(map.hasLayer(SHP_layer1)){
        SHP_layer1.setOpacity(0)
    }
    if(map.hasLayer(SHP_layer2)){
        SHP_layer2.setOpacity(0)
    }
}


/********************************************背景地图切换****************************************************/
// 切换为fault_map，将fault_map的opacity设为1，其他的opacity设为0
function changeto_default_map(){
    default_map.setOpacity(1);
    google_map.setOpacity(0);
    google_satellite.setOpacity(0);
    gaode_satellite.setOpacity(0);
    // gaodebiaozhu.setOpacity(0);
    // maoer_theme_name.setZIndex(10);//设置林场名称图层zindex为10
}
// 切换为google_map
function changeto_google_map(){
    default_map.setOpacity(0);
    google_map.setOpacity(1);
    google_satellite.setOpacity(0);
    gaode_satellite.setOpacity(0);
    // gaodebiaozhu.setOpacity(0);
    // maoer_theme_name.setZIndex(10);//设置林场名称图层zindex为10
}

function changeto_google_sat(){
    default_map.setOpacity(0);
    google_map.setOpacity(0);
    google_satellite.setOpacity(1);
    gaode_satellite.setOpacity(0);
    // gaodebiaozhu.setOpacity(1);
    // maoer_theme_name.setZIndex(10);//设置林场名称图层zindex为10
}

function changeto_gaode_sat(){
    default_map.setOpacity(0);
    google_map.setOpacity(0);
    google_satellite.setOpacity(0);
    gaode_satellite.setOpacity(1);
    // gaodebiaozhu.setOpacity(1);
    // maoer_theme_name.setZIndex(10);//设置林场名称图层zindex为10
}

/*********************************************组件加载********************************************************/
function loadComponents() {
    //图层切换
    var baseMaps = {
        // "China":China,
        // "ChinaDark":ChinaDark
    };
    var overlayMaps = {
        // "China":China,
        // "ChinaDark":ChinaDark,
        "标注": gaodebiaozhu,
        "林场名": maoer_theme_name
        

    };
    L.control.layers(baseMaps, overlayMaps).addTo(map);
    //比例尺
    L.control.scale({position:'bottomleft'}).addTo(map);
    //鹰眼图
    var osm2 = L.supermap.tiledMapLayer("http://ec2-52-80-7-103.cn-north-1.compute.amazonaws.com.cn:8090/iserver/services/map-china400/rest/maps/China", {minZoom: 5, maxZoom: 13,noWrap: true});
    // L.control.minimap(osm2, {mapOptions:{ logoControl: true},toggleDisplay: true}).addTo(map);
    //缩放控件
    L.control.zoom({position:'topright'}).addTo(map);
}

//获取价值统计
function allData(array){
    let send=JSON.stringify(array)
    $.ajax({ 
        type: "POST",   
        url: "/linda/magnitude/value/summary",
        data: send,
        dataType: "json",
        contentType:"application/json",
        success: function(data){
            if (data.code == 200) { 
                allChart(data.data)
            } else {
                console.log('出现错误')
            }  
        },
        error: function(jqXHR){     
            console.log("发生错误");  
        },     
    });
  }